package com.ruoyi.teaching.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.teaching.dto.AddHolidayDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaHolidayConfigMapper;
import com.ruoyi.teaching.domain.YaHolidayConfig;
import com.ruoyi.teaching.service.IYaHolidayConfigService;

/**
 * 节假日配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-29
 */
@Service
public class YaHolidayConfigServiceImpl implements IYaHolidayConfigService
{
    @Autowired
    private YaHolidayConfigMapper yaHolidayConfigMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询节假日配置
     *
     * @param id 节假日配置主键
     * @return 节假日配置
     */
    @Override
    public YaHolidayConfig selectYaHolidayConfigById(Long id)
    {
        return yaHolidayConfigMapper.selectYaHolidayConfigById(id);
    }

    /**
     * 查询节假日配置列表
     *
     * @param yaHolidayConfig 节假日配置
     * @return 节假日配置
     */
    @Override
    public List<YaHolidayConfig> selectYaHolidayConfigList(YaHolidayConfig yaHolidayConfig)
    {
        return yaHolidayConfigMapper.selectYaHolidayConfigList(yaHolidayConfig);
    }

    /**
     * 新增节假日配置
     *
     * @param yaHolidayConfig 节假日配置
     * @return 结果
     */
    @Override
    public int insertYaHolidayConfig(YaHolidayConfig yaHolidayConfig)
    {
        // 将获取到数据转换为对象
        String content = yaHolidayConfig.getContent();
        try {
            // 转换为JSON
            List<AddHolidayDTO> addHolidayDTOS = JSON.parseArray(content, AddHolidayDTO.class);
            LocalDate startDate = addHolidayDTOS.get(6).getStartDate();
            // 将日期转换为 "2025-11-12"
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String format = formatter.format(startDate);
            // 设置年份
            String year = format.split("-")[0];
            yaHolidayConfig.setYear(year);

            // # 避免重复新增 (①、查询  ②、先删除在新增-根据年份删除即可)
            yaHolidayConfigMapper.deleteYaHolidayConfigByYear(year);
            stringRedisTemplate.delete("holiday:" + year);
            HashMap<String, String> holidayConfig = new HashMap<>();
            for (AddHolidayDTO addHolidayDTO : addHolidayDTOS){
                for (int i = 0; i < addHolidayDTO.getDays(); i++) {
                    LocalDate cDate = addHolidayDTO.getStartDate();
                    holidayConfig.put(cDate.toString(), addHolidayDTO.getName());
                    cDate.plusDays(1);
                }
            }
            // 生成具体节假器配置信息
            stringRedisTemplate.opsForHash().putAll("holiday:" + yaHolidayConfig.getYear(), holidayConfig);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return yaHolidayConfigMapper.insertYaHolidayConfig(yaHolidayConfig);
    }

    /**
     * 修改节假日配置
     *
     * @param yaHolidayConfig 节假日配置
     * @return 结果
     */
    @Override
    public int updateYaHolidayConfig(YaHolidayConfig yaHolidayConfig)
    {
        //  删除redis中数据
        stringRedisTemplate.delete("holiday:" + yaHolidayConfig.getYear());
        // 添加即可
        String content = yaHolidayConfig.getContent();
        // 转换为JSON
        List<AddHolidayDTO> addHolidayDTOS = JSON.parseArray(content, AddHolidayDTO.class);
        HashMap<String, String> holidayConfig = new HashMap<>();
        for (AddHolidayDTO addHolidayDTO : addHolidayDTOS){
            for (int i = 0; i < addHolidayDTO.getDays(); i++) {
                LocalDate cDate = addHolidayDTO.getStartDate();
                holidayConfig.put(cDate.toString(), addHolidayDTO.getName());
                cDate.plusDays(1);
            }
        }
        stringRedisTemplate.opsForHash().putAll("holiday:" + yaHolidayConfig.getYear(), holidayConfig);
        return yaHolidayConfigMapper.updateYaHolidayConfig(yaHolidayConfig);
    }

    /**
     * 批量删除节假日配置
     *
     * @param ids 需要删除的节假日配置主键
     * @return 结果
     */
    @Override
    public int deleteYaHolidayConfigByIds(Long[] ids)
    {
        return yaHolidayConfigMapper.deleteYaHolidayConfigByIds(ids);
    }

    /**
     * 删除节假日配置信息
     *
     * @param id 节假日配置主键
     * @return 结果
     */
    @Override
    public int deleteYaHolidayConfigById(Long id)
    {
        // 删除redis中数据
        stringRedisTemplate.delete("holiday:" + yaHolidayConfigMapper.selectYaHolidayConfigById(id).getYear());

        return yaHolidayConfigMapper.deleteYaHolidayConfigById(id);
    }
}
